﻿/* Nibiru Studio Engine Version [1062] */
#include "SceneCapture.h"
#include "EyeManager.h"

ENGINE_NAMESPACE_BEGIN

IMPLEMENT_CLASS(SceneCapture)
IMPLEMENT_RTTI_CUSTOM(SceneCapture, NComponent)

//ENUM_BEGIN(LabelType, LabelType)
// add your enum 
// Display Name, Enum Type Name
// ENUM(Left, LEFT)
//ENUM_END


REFLECTION_BEGIN(SceneCapture)
// add your variable reflection property 
//Enum Class Name, Display Name In Editor, Variable Name In Class, flags default is 0
//ENUM_PROPERTY(LabelType, Type, m_LabelType, 0)
//PROPERTY(Bold, m_IsBold, 0)
//PROPERTY(Rotate, m_EnableRotate, 0)
//FUNCTION(OnButtonClick, SceneCapture, OnClick)

REFLECTION_END


void SceneCapture::Start()
{
    m_captureIndex = 0;
    //填充采集点位置
    m_captureDotList.clear();
    m_captureDotList.push_back(pair(0, 0));
    //
    m_captureDotList.push_back(pair(-0.5f, 0.5f));
    m_captureDotList.push_back(pair(0.5f, 0.5f));
    m_captureDotList.push_back(pair(0.5f, -0.5f));
    m_captureDotList.push_back(pair(-0.5f, -0.5f));
    //
    m_captureDotList.push_back(pair(0, 1));
    m_captureDotList.push_back(pair(1, 0));
    m_captureDotList.push_back(pair(0, -1));
    m_captureDotList.push_back(pair(-1, 0));
    //
    m_captureDotList.push_back(pair(-1.5f, 1.5f));
    m_captureDotList.push_back(pair(1.5f, 1.5f));
    m_captureDotList.push_back(pair(1.5f, -1.5f));
    m_captureDotList.push_back(pair(-1.5f, -1.5f));
    //
    m_captureDotList.push_back(pair(0, 1.9f));
    m_captureDotList.push_back(pair(2, 0));
    m_captureDotList.push_back(pair(0, -1.9f));
    m_captureDotList.push_back(pair(-2, 0));
    //
    m_captureDotList.push_back(pair(-2.5f, 1.9f));
    m_captureDotList.push_back(pair(2.5f, 1.9f));
    m_captureDotList.push_back(pair(2.5f, -1.9f));
    m_captureDotList.push_back(pair(-2.5f, -1.9f));
    ////
    //m_captureDotList.push_back(pair(0, 3));
    //m_captureDotList.push_back(pair(3, 0));
    //m_captureDotList.push_back(pair(0, -3));
    //m_captureDotList.push_back(pair(-3, 0));
    ////
    //m_captureDotList.push_back(pair(-3.5f, 3.5f));
    //m_captureDotList.push_back(pair(3.5f, 3.5f));
    //m_captureDotList.push_back(pair(3.5f, -3.5f));
    //m_captureDotList.push_back(pair(-3.5f, -3.5f));
    ////
    //m_captureDotList.push_back(pair(0, 4));
    //m_captureDotList.push_back(pair(4, 0));
    //m_captureDotList.push_back(pair(0, -4));
    //m_captureDotList.push_back(pair(-4, 0));


    m_pEyeManager = EyeManager::instance();
    m_textMessage = NActorManager::GetActor("text_message")->GetComponent<NLabel>();
    //
    m_capturePoint = NActorManager::GetActor("img_capture_point");
    pair<float, float> position = m_captureDotList.at(m_captureIndex);
    char buf[14] = { 0 };
    sprintf(buf, "%04.0f.%04.0f", (5000 + position.first * 500.0f), (5000 + position.second * 500.0f));
    m_capPosStr = buf;
    m_capturePoint->SetLocalPosition(Vector3(position.first, position.second, 0));
    m_capScale = 1;
    m_capScaleValid = true;
    //
    ServiceResult serviceResult = m_pEyeManager->registerImageCallback();
    if (serviceResult.code != 0)
    {
        NDebug::Log("%s registerImageCallback error, %s", EYE_TAG, serviceResult.toString().c_str());
        m_textMessage->SetText(serviceResult.toString());
    }

}

void SceneCapture::Update()
{
    if (NInput::GetKeyDown(KeyboardKeys::Android_DpadLeft)) // 左键重新采图
    {
        m_textMessage->SetText("");
        m_captureIndex = 0;
        pair<float, float> position = m_captureDotList.at(m_captureIndex);
        char buf[14] = { 0 };
        sprintf(buf, "%04.0f.%04.0f", (5000 + position.first * 500.0f), (5000 + position.second * 500.0f));
        m_capPosStr = buf;
        m_capturePoint->SetLocalPosition(Vector3(position.first, position.second, 0));
        m_capScale = 1;
        m_capScaleValid = true;
    }
    else if (NInput::GetKeyDown(KeyboardKeys::Android_DpadRight)) // 右键采集点跳转下一个位置
    {
        m_captureIndex++;
        if (m_captureIndex >= m_captureDotList.size())
        {
            m_capPosStr = "none";
            m_capturePoint->SetLocalPosition(Vector3(-5, 3, 0));
            m_textMessage->SetText("Capture finished");
            return;
        }
        pair<float, float> position = m_captureDotList.at(m_captureIndex);
        char buf[14] = { 0 };
        sprintf(buf, "%04.0f.%04.0f", (5000 + position.first * 500.0f), (5000 + position.second * 500.0f));
        m_capPosStr = buf;
        m_capturePoint->SetScale(1);
        m_capturePoint->SetLocalPosition(Vector3(position.first, position.second, 0));
        m_capScale = 1;
        m_capScaleValid = true;
    }
    else if (NInput::GetKeyDown(KeyboardKeys::Android_DpadDown)) // 下键开始单张采图
    {
        saveImage();
    }
    else if (NInput::GetKeyDown(KeyboardKeys::Android_Back)) // 返回键返回校准界面
    {
        switchToCalib();
    }
    updateScale();
}

void SceneCapture::OnDestroy()
{
    ServiceResult serviceResult = m_pEyeManager->unregisterImageCallback();
    if (serviceResult.code != 0)
    {
        NDebug::Log("%s unregisterImageCallback error, %s", EYE_TAG, serviceResult.toString().c_str());
    }
    NDebug::Log("%s capture destroy", EYE_TAG);
}

void SceneCapture::PostEditChangeProperty(std::string& propertyName)
{
	// Variable Property changed in Editor
}

void SceneCapture::Serialize(Archive& ar)
{
	Super::Serialize(ar);
	// SERIALIZE(m_EnableRotate);
}

void SceneCapture::LateDeserialize(Archive& ar)
{
	// After all Serialize are executed
	Super::LateDeserialize(ar);
}

void SceneCapture::switchToCalib()
{
    NLevelManager::LoadLevel("scene_calib.nscene");
}

void SceneCapture::updateScale()
{
    if (!m_capScaleValid)
    {
        return;
    }

    m_capScale -= 0.01f;
    m_capturePoint->SetScale(m_capScale);
    if (m_capScale < 0.45f)
    {
        m_capScaleValid = false;
    }
}

void SceneCapture::saveImage()
{
    m_textMessage->SetText("");
    ServiceResult serviceResult = m_pEyeManager->saveImage(m_capPosStr);
    if (serviceResult.code != 0)
    {
        NDebug::Log("%s startCalibration error, %s", EYE_TAG, serviceResult.toString().c_str());
        m_textMessage->SetText(serviceResult.toString());
    }
}

ENGINE_NAMESPACE_END
